package com.webssky.jcseg.main;

import java.io.IOException;
import java.io.StringReader;

import com.webssky.jcseg.core.ISegment;
import com.webssky.jcseg.core.IWord;
import com.webssky.jcseg.core.SegmentFacotry;

/**
 * jcseg demo.
 * 
 * @author chenxin
 * {@link http://www.webssky.com}
 */
public class JSeg {
	
	ISegment seg = null;
	
	public JSeg() {
		seg = SegmentFacotry.createSegment("com.webssky.jcseg.ComplexSeg");
	}
	
	public String segment(String str) throws IOException {
		
		StringBuffer sb = new StringBuffer();
		//seg.setLastRule(null);
		IWord word = null;
		
//		long _start = System.nanoTime();
		boolean isFirst = true;
		int counter = 0;
		seg.reset(new StringReader(str));
		while ( (word = seg.next()) != null ) {
			if ( isFirst ) {
				sb.append(word.getValue());
				isFirst = false;
			}
			else {
				sb.append(" ");
				sb.append(word.getValue());
			}
			//clear the allocations of the word.
			word = null;
			counter++;
		}
		long e = System.nanoTime();
		return sb.toString();
//		System.out.println(seg.getStreamPosition());
//		System.out.format(seg.getStreamPosition()+", split:" +
//				+counter+", cost: %.5fsec\n", ((float)e - _start)/1E9);
	}

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		FileArrayProvider fap = new FileArrayProvider();
//		String[]lines = fap.readLines("/home/eric/tmp/index.html");
		String s = "你好么";
		JSeg demo = new JSeg();
		
		
		System.out.println(demo.segment(s));

	}

}
